package org.example;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 15. 三数之和
 */
public class ThreeSum {

    public List<List<Integer>> threeSum(int[] nums) {
        int length = nums.length;
        if (nums.length < 3) {
            return List.of();
        }
        Arrays.sort(nums);
        Set<List<Integer>> set = new HashSet<>();
        for (int first = 0; first < length; first++) {
            if (first > 0 && nums[first] == nums[first - 1]) {
                continue;
            }
            int target = -nums[first];
            int third = length - 1;
            for (int second = first + 1; second < length; second++) {
                if (second > first + 1 && nums[second] == nums[second - 1]) {
                    continue;
                }
                while (second < third && nums[second] + nums[third] > target) {
                    third--;
                }
                if(second == third) {
                    break;
                }
                if (nums[second] + nums[third] == target) {
                    set.add(List.of(nums[first], nums[second], nums[third]));
                }
            }
        }
        return set.stream().toList();
    }


    /**
     * 超时,暴力破解法
     */
    public List<List<Integer>> threeSumSecond(int[] nums) {
        if (nums.length < 3) {
            return List.of();
        }
        Set<List<Integer>> set = new HashSet<>();
        nums = Arrays.stream(nums).sorted().toArray();
        for (int i = 0; i < nums.length - 2; i++) {
            for (int j = i + 1; j < nums.length - 1; j++) {
                for (int k = j + 1; k < nums.length; k++) {
                    if (nums[i] + nums[j] + nums[k] == 0) {
                        set.add(List.of(nums[i], nums[j], nums[k]));
                    }
                }
            }
        }
        return set.stream().toList();
    }

    /**
     * 结果不对
     */
    public List<List<Integer>> threeSumFirst(int[] nums) {
        if (nums.length < 3) {
            return List.of();
        }
        nums = Arrays.stream(nums).sorted().toArray();
        for (int i : nums) {
            System.out.println(i);
        }
        int l = 0, r = nums.length - 1;
        Set<List<Integer>> set = new HashSet<>();
        while (r > l) {
            int result = nums[l] + nums[r];
            if (result > 0) {
                for (int j = l + 1; j < r; j++) {
                    if (nums[j] + result == 0) {
                        set.add(List.of(nums[l], nums[r], nums[j]));
                        break;
                    }
                }
                r--;
            } else if (result < 0) {
                for (int j = r - 1; j > l; j--) {
                    if (nums[j] + result == 0) {
                        set.add(List.of(nums[l], nums[r], nums[j]));
                        break;
                    }
                }
                l++;
            } else {
                for (int j = l + 1; j < r; j++) {
                    if (nums[j] + result == 0) {
                        set.add(List.of(nums[l], nums[r], nums[j]));
                        break;
                    }
                }
                if (nums[l + 1] - nums[l] > nums[r] - nums[r - 1]) {
                    r--;
                } else {
                    l++;
                }
            }

        }
        return set.stream().toList();
    }

    public static void main(String[] args) {
//        int[] nums = {-1, 0, 1, 2, -1, -4};
//        ThreeSum threeSum = new ThreeSum();
//        List<List<Integer>> lists = threeSum.threeSumFirst(nums);
//        System.out.println(lists);


        int[] nums = {3, 0, -2, -1, 1, 2};
        ThreeSum threeSum = new ThreeSum();
        List<List<Integer>> lists = threeSum.threeSumFirst(nums);
        System.out.println(lists);
    }
}
